<template>
  <div class="order-list">
    <el-card>
      <h2>充电订单</h2>
      <el-table :data="orders" style="width:100%">
        <el-table-column prop="chargingID" label="订单编号" />
        <el-table-column prop="pileID" label="充电桩编号" />
        <el-table-column prop="chargingtype" label="充电类型" />
        <el-table-column prop="chargingtime" label="时长(h)" />
        <el-table-column prop="chargingcost" label="费用(元)" />
        <el-table-column prop="paytstatus" label="支付状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.paytstatus ? 'success' : 'info'">
              {{ scope.row.paytstatus ? '已支付' : '未支付' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="!scope.row.paytstatus" size="mini" type="primary" @click="payOrder(scope.row)">支付</el-button>
            <el-button v-if="scope.row.paytstatus && scope.row.refundable" size="mini" type="warning" @click="refundOrder(scope.row)">退款</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="onAddOrder">新建充电订单</el-button>
    </el-card>
    <el-dialog :visible.sync="dialogVisible" title="新建充电订单">
      <el-form :model="form" label-width="80px">
        <el-form-item label="充电桩">
          <el-select v-model="form.pileID" placeholder="请选择充电桩">
            <el-option v-for="p in piles" :key="p.pileID" :label="p.pileID + '（' + p.piletype + '）'" :value="p.pileID" />
          </el-select>
        </el-form-item>
        <el-form-item label="充电类型">
          <el-select v-model="form.chargingtype" placeholder="请选择类型">
            <el-option label="快充" value="快充" />
            <el-option label="慢充" value="慢充" />
          </el-select>
        </el-form-item>
        <el-form-item label="时长(h)">
          <el-input v-model="form.chargingtime" type="number" min="1" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="saveOrder">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api';
export default {
  data() {
    return {
      orders: [],
      dialogVisible: false,
      form: { pileID: '', chargingtype: '', chargingtime: 1 },
      piles: []
    };
  },
  async created() {
    await this.fetchOrders();
  },
  methods: {
    async fetchOrders() {
      this.orders = await api.get('/charging/list');
    },
    async onAddOrder() {
      this.piles = await api.get('/pile/list');
      this.form = { pileID: '', chargingtype: '', chargingtime: 1 };
      this.dialogVisible = true;
    },
    async saveOrder() {
      await api.post('/charging/add', this.form);
      this.dialogVisible = false;
      await this.fetchOrders();
    },
    async payOrder(order) {
      await api.post('/charging/pay', { chargingID: order.chargingID });
      await this.fetchOrders();
    },
    async refundOrder(order) {
      await api.post('/charging/refund', { chargingID: order.chargingID });
      await this.fetchOrders();
    }
  }
};
</script>

<style scoped>
.order-list { max-width: 900px; margin: 40px auto; }
</style>
